package com.heima.crm.controller;

import com.heima.crm.common.result.Result;
import com.heima.crm.pojos.dtos.ResetPasswordDto;
import com.heima.crm.pojos.entity.TbUser;
import com.heima.crm.pojos.user.DeleteDTO;
import com.heima.crm.pojos.user.UserPageDto;
import com.heima.crm.pojos.user.SaveUserDto;
import com.heima.crm.service.CrUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/user")
public class CrUserController {
    @Resource
    private CrUserService crUserService;

    /**
     * 动态分页
     * @return
     */
    @PostMapping("/page")
    public Result page(@RequestBody UserPageDto userPageDto){
        log.info("动态分页{}", userPageDto);
        return crUserService.page(userPageDto);
    }

    @GetMapping("/getDept")
    public Result getDept(){
        log.info("部门回显");
        return crUserService.getDept();
    }
    @GetMapping("/getPost")
    public Result getPost(){
        log.info("岗位回显");
        return crUserService.getPost();
    }

    /**
     * 新增用户
     * @param saveUserDto
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody SaveUserDto saveUserDto){
        log.info("新增用户{}",saveUserDto);
        return crUserService.save(saveUserDto);
    }

    /**
     * 导入用户
     * @param excelFile
     * @return
     */
    @GetMapping("/importUsers")
    public Result importUsers(MultipartFile excelFile){
        log.info("导入用户,{}",excelFile);
        return crUserService.importUsers(excelFile);
    }

    //确认导入
    @GetMapping("/sure")
    public Result sure(String kid){
        log.info("确认导入{}",kid);
        return crUserService.sure(kid);
    }

    //删除导入数据
    @GetMapping("/unsure")
    public Result unsure (String kid){
        log.info("不导入{}",kid);
        return crUserService.unsure(kid);
    }

    /**
     * 导出用户
     * @return
     */
    @GetMapping("/export")
    public Result export(HttpServletResponse response){
        log.info("导出用户");
        return crUserService.export(response);
    }

    /**
     * 数据回显
     * @param userId
     * @return
     */
    @GetMapping("/getuser/{userId}")
    public Result getUser(@PathVariable Long userId){
        log.info("数据回显{}",userId);
        return crUserService.getUser(userId);
    }

    /**
     * 修改用户信息
     * @param tbUser
     * @return
     */
    @PutMapping("/updateUser")
    public Result updateUser(@RequestBody TbUser tbUser){
        log.info("修改用户信息{}",tbUser);
        return crUserService.updateUser(tbUser);
    }

    /**
     * 删除用户
     * @param userId
     * @return
     */
    @DeleteMapping("/deleteUser")
    public Result deleteUser(@RequestParam List<Long> userId){
        log.info("删除用户{}",userId);
        return crUserService.deleteUserById(userId);
    }


    /**
     * 退出登录
     * @return
     */
    @DeleteMapping("/loginOut")
    public Result loginOut(){
        log.info("退出登录");
        return Result.success();
    }
}
